package com.slipper.common.bo;

import com.slipper.common.constants.Constants;
import com.slipper.common.util.GsonUtil;

import java.util.*;

public class Result<T> {
    private T data;
    private String code;
    private String message;
    private Integer pageNumber;
    private Integer pageSize;
    private Integer totalPage;
    private Long totalRecord;

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Integer getPageNumber() {
        return pageNumber;
    }

    public void setPageNumber(Integer pageNumber) {
        this.pageNumber = pageNumber;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(Integer totalPage) {
        this.totalPage = totalPage;
    }

    public Long getTotalRecord() {
        return totalRecord;
    }

    public void setTotalRecord(Long totalRecord) {
        this.totalRecord = totalRecord;
    }

    public boolean isSuccess() {
        return Constants.YES.equals(this.code);
    }

    public String toString() {
        Map<String, Object> result = new LinkedHashMap<>();
        result.put(Constants.DATA_NAME, this.getData());
        result.put(Constants.CODE_NAME, this.getCode());
        result.put(Constants.MSG_NAME, this.getMessage());

        addNotNull(result, Constants.PAGE_NUMBER_NAME, this.getPageNumber());
        addNotNull(result, Constants.PAGE_SIZE_NAME, this.getPageSize());
        addNotNull(result, Constants.TOTAL_PAGE_NAME, this.getTotalPage());
        addNotNull(result, Constants.TOTAL_RECORD_NAME, this.getTotalRecord());

        return GsonUtil.toJson(result);
    }

    private void addNotNull(Map<String, Object> map, String key, Object value) {
        if (value != null) {
            map.put(key, value);
        }
    }
}
